package com.xxx.dorm.service.impl;

import com.xxx.dorm.dao.BuildingDao;
import com.xxx.dorm.dao.DormitoryDao;
import com.xxx.dorm.dao.StudentDao;
import com.xxx.dorm.dao.impl.BuildingDaoImpl;
import com.xxx.dorm.dao.impl.DormitoryDaoImpl;
import com.xxx.dorm.dao.impl.StudentDaoImpl;
import com.xxx.dorm.domain.Building;
import com.xxx.dorm.domain.Dormitory;
import com.xxx.dorm.domain.Student;
import com.xxx.dorm.service.StudentService;

import java.util.List;

public class StudentServiceImpl implements StudentService {

    private StudentDao studentDao = new StudentDaoImpl();
    private DormitoryDao dormitoryDao = new DormitoryDaoImpl();
    private BuildingDao buildingDao = new BuildingDaoImpl();

    @Override
    public List<Student> findAll() {
        return studentDao.findAll();
    }

    @Override
    public void addStudent(Student student) {
        dormitoryDao.addActualPeople(student.getDid());
        Dormitory dormitory = dormitoryDao.findByDid(student.getDid());
        buildingDao.addActualPeople(dormitory.getBid());
        //调用dao
        studentDao.addStudent(student);
    }

    /**
     * 修改学生信息
     * @param student
     * @param beforeDid
     */
    @Override
    public void updateStudent(Student student,Integer beforeDid) {
        //减少原来的实际人数
        dormitoryDao.reduceActualPeople(beforeDid);
        Dormitory dormitory = dormitoryDao.findByDid(beforeDid);
        buildingDao.reduceActualPeople(dormitory.getBid());
        //增加新增的实际人数
        dormitoryDao.addActualPeople(student.getDid());
        dormitory = dormitoryDao.findByDid(student.getDid());
        buildingDao.addActualPeople(dormitory.getBid());
        studentDao.updateStudent(student);
    }

    @Override
    public Student findById(Integer id) {
        return studentDao.findById(Integer.valueOf(id));
    }

    /**
     * 删除学生
     * @param id
     */
    @Override
    public void removeStudent(String id) {
        Student student = studentDao.findById(Integer.valueOf(id));
        dormitoryDao.reduceActualPeople(student.getDid());
        Dormitory dormitory = dormitoryDao.findByDid(student.getDid());
        buildingDao.reduceActualPeople(dormitory.getBid());
        studentDao.removeStudent(id);
    }

    /**
     * 带条件查找
     * @param conditions
     * @param information
     * @return
     */
    @Override
    public List<Student> find(String conditions, String information) {
        return studentDao.find(conditions,information);
    }


}
